package com.example.reggie.Filter;

import com.alibaba.fastjson.JSON;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.util.AntPathMatcher;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*过滤器为了实现客户必须登录*/
/*拦截所有*/

@WebFilter("/*")
/*日志*/
@Slf4j
@EnableSwagger2
@EnableKnife4j
public class LoginCheckFilter implements Filter {

    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;

//        获取到本次请求的uri地址
        String uri = request.getRequestURI();

//        输出
        log.info("本次访问的uri地址:{}",uri);


        //定义不需要处理的请求路径
        String[] urls = new String[]{
                "/employee/login",
                "/employee/logout",
                "/user/login",
                "/user/sendMsg",
                "/backend/**",
                "/front/**",
                "/imgs/**",

                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"

        };

        boolean match=check(uri,urls);

//        true的情况下可以直接放行
        if (match){
            log.info("本次访问的uri地址可以直接放行:{}",uri);
            filterChain.doFilter(request,response);
//            不加return会导致接下来的语句也执行
            return;
        }

//        判断是否登录
        if (request.getSession().getAttribute("employee")!=null){
            Long employee = (Long)request.getSession().getAttribute("employee");
            /*放入ThreadLocal*/
            BaseContext.setThreadLocal(employee);

            filterChain.doFilter(request,response);
            return;
        }

        /*判断是否登录,手机*/
        if(request.getSession().getAttribute("user") != null){
            log.info("用户已登录，用户id为：{}",request.getSession().getAttribute("user"));

            long userId = (long) request.getSession().getAttribute("user");

            BaseContext.setThreadLocal(userId);

            filterChain.doFilter(request,response);
            return;
        }


        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;

    }

    /*路径匹配，检查本次请求是否需要放行 */
    private boolean check(String uri, String[] urls) {

        for (String url : urls) {
            if (PATH_MATCHER.match(url,uri)){
                return true;
            }
        }
        return false;

    }
}
